package com.unit6;

import com.unit6.util.GetConnection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author 李琦 on 2021/8/17.
 * @version java version "16.0.1" 2021-04-20
 * 使用公共的连接类来
 * 读取数据库中的内容
 */
public class test3 {
    public static void main(String[] args){
        Connection conn=null;
        Statement stmt=null;
        try{
            conn= GetConnection.getConn();//获取公共的连接对象
            String sql="select * from stu";
            //Statement对象用来执行sql语句
            stmt=conn.createStatement();
            //执行sql并获取查询结果
            ResultSet rs=stmt.executeQuery(sql);
            while ((rs.next())){
                System.out.print("id: "+rs.getInt("id")+"  ");
                System.out.print("名字: "+rs.getString("name")+"  ");
                System.out.print("年龄: "+rs.getInt("age")+"  ");
                System.out.print("性别: "+rs.getString("sex")+"\n");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }



    }
}
